using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Areas.Admin.Consts;
using FiltronASP.Areas.Admin.ViewModels.Menus;
using FiltronASP.Helpers.ActionFilters;
using FiltronASP.Helpers.Pagination;
using FiltronASP.Helpers.StringExtensions;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.Areas.Admin.Helpers.Permissions;

namespace FiltronASP.Areas.Admin.Controllers
{
    public class MenusController : BaseController
    {

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela pozycji menu
        /// </summary>
        protected IMenuItemDAO _menuItemDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela typów pozycji menu
        /// </summary>
        protected IMenuItemTypeDAO _menuItemTypeDAO;

        /// <summary>
        /// klasa odpowiedzialna za operacja na bazie danych, tabela stron
        /// </summary>
        protected IPageDAO _pageDAO;

        /// <summary>
        /// Konstruktor.
        /// </summary>
        /// <param name="menuItemDAO">klasa odpowiedzialna za operacja na bazie danych, tabela pozycji menu</param>
        /// <param name="menuItemTypeDAO">klasa odpowiedzialna za operacja na bazie danych, tabela typów pozycji menu</param>
        /// <param name="pageDAO">klasa odpowiedzialna za operacja na bazie danych, tabela stron</param>
        /// <param name="pageTypeDAO">klasa odpowiedzialna za operacja na bazie danych, tabela grup stron</param>
        public MenusController(IMenuItemDAO menuItemDAO, IMenuItemTypeDAO menuItemTypeDAO, 
            IPageDAO pageDAO)
        {
            this._menuItemDAO = menuItemDAO;
            this._menuItemTypeDAO = menuItemTypeDAO;
            this._pageDAO = pageDAO;
        }

        /// <summary>
        /// Konstruktor.
        /// </summary>
        public MenusController()
            : this(new MenuItemDAO(), new MenuItemTypeDAO(), 
                new PageDAO())
        {
        }

        /// <summary>
        /// Metoda pomocnicza do pobierania modelu <code>MenusIndexViewModel</code>.
        /// </summary>
        /// <param name="currentLanguageId">biezacy jezyk</param>
        /// <returns>odpowiedni model danych</returns>
        private MenusIndexViewModel GetMenusIndexViewModel(int? currentLanguageId)
        {
            Language currentLanguage = this.GetCurrentLanguage(currentLanguageId);
            Menu currentMenu = this._menuDAO.GetDefaultMenu(currentLanguage);
            if (currentMenu != null)
            {
                return new MenusIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                        this.GetPageTypesInMenu(currentLanguageId),
                        this.GetMenus(currentLanguageId),
                        this.GetCurrentLanguage(currentLanguageId),
                        Pagination.ToPagedList<MenuItem>(this._menuItemDAO.FindByMenuIdAndDepth(currentMenu.menu_id, 0), 1),
                        this.GetMenuTypesToDropDown(null),
                        this.GetCurrentMenu(currentMenu.menu_id),
                        null);  //item
            }   //if menu id
            return new MenusIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                        this.GetPageTypesInMenu(currentLanguageId),
                        this.GetMenus(currentLanguageId),
                        this.GetCurrentLanguage(currentLanguageId),
                        null,   //menu items
                        this.GetMenuTypesToDropDown(null),
                        null,   //current menu
                        null);  //current item
        }

        /// <summary>
        /// Metoda pomocnicza do pobierania modelu <code>MenusIndexViewModel</code>.
        /// </summary>
        /// <param name="menuId">aktualne menu</param>
        /// <param name="currentLanguageId">biezacy jezyk</param>
        /// <returns>odpowiedni model danych</returns>
        private MenusIndexViewModel GetMenusIndexViewModel(int? menuId, int? currentLanguageId)
        {
            if (menuId.HasValue)
            {
                return new MenusIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                        this.GetPageTypesInMenu(currentLanguageId),
                        this.GetMenus(currentLanguageId),
                        this.GetCurrentLanguage(currentLanguageId),
                        Pagination.ToPagedList<MenuItem>(this._menuItemDAO.FindByMenuIdAndDepth(menuId.Value, 0), 1),
                        this.GetMenuTypesToDropDown(null),
                        this.GetCurrentMenu(menuId),
                        null);  //item
            }   //if menu id
            return new MenusIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                        this.GetPageTypesInMenu(currentLanguageId),
                        this.GetMenus(currentLanguageId),
                        this.GetCurrentLanguage(currentLanguageId),
                        null,   //menu items
                        this.GetMenuTypesToDropDown(null),
                        null,   //current menu
                        null);  //current item
        }

        /// <summary>
        /// Metoda pomocnicza do pobierania modelu <code>MenusIndexViewModel</code>.
        /// </summary>
        /// <param name="menuId">aktualne menu</param>
        /// <param name="menuItemId">Aktualne nadrzedny element (folder)</param>
        /// <param name="currentLanguageId">biezacy jezyk</param>
        /// <returns>odpowiedni model danych</returns>
        private MenusIndexViewModel GetMenusIndexViewModel(int? menuId, int? menuItemId, int? currentLanguageId)
        {
            if (menuId.HasValue)
            {
                if (menuItemId.HasValue)
                {
                    return new MenusIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                            this.GetPageTypesInMenu(currentLanguageId),
                            this.GetMenus(currentLanguageId),
                            this.GetCurrentLanguage(currentLanguageId),
                            Pagination.ToPagedList<MenuItem>(this._menuItemDAO.FindByParentId(menuItemId.Value), 1),
                            this.GetMenuTypesToDropDown(null),
                            this.GetCurrentMenu(menuId),
                            this.GetCurrentMenuItem(menuItemId));
                }   //if item id
                return new MenusIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                        this.GetPageTypesInMenu(currentLanguageId),
                        this.GetMenus(currentLanguageId),
                        this.GetCurrentLanguage(currentLanguageId),
                        Pagination.ToPagedList<MenuItem>(this._menuItemDAO.FindByMenuIdAndDepth(menuId.Value, 0), 1),
                        this.GetMenuTypesToDropDown(null),
                        this.GetCurrentMenu(menuId),
                        null);  //item
            }   //if menu id
            return new MenusIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                        this.GetPageTypesInMenu(currentLanguageId),
                        this.GetMenus(currentLanguageId),
                        this.GetCurrentLanguage(currentLanguageId),
                        null,   //menu items
                        this.GetMenuTypesToDropDown(null),
                        null,   //current menu
                        null);  //current item
        }

        /// <summary>
        /// Metoda pomocnicza do pobierania modelu <code>MenusIndexViewModel</code>.
        /// </summary>
        /// <param name="page">biezaca strona</param>
        /// <param name="menuId">aktualne menu</param>
        /// <param name="menuItemId">Aktualne nadrzedny element (folder)</param>
        /// <param name="currentLanguageId">biezacy jezyk</param>
        /// <returns>odpowiedni model danych</returns>
        private MenusIndexViewModel GetMenusIndexViewModel(int? page, int? menuId, int? menuItemId, int? currentLanguageId)
        {
            if (menuId.HasValue)
            {
                if (menuItemId.HasValue)
                {
                    return new MenusIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                            this.GetPageTypesInMenu(currentLanguageId),
                            this.GetMenus(currentLanguageId),
                            this.GetCurrentLanguage(currentLanguageId),
                            Pagination.ToPagedList<MenuItem>(this._menuItemDAO.FindByParentId(menuItemId.Value), page ?? 1),
                            this.GetMenuTypesToDropDown(null),
                            this.GetCurrentMenu(menuId),
                            this.GetCurrentMenuItem(menuItemId), 
                            page);
                }   //if item id
                return new MenusIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                        this.GetPageTypesInMenu(currentLanguageId),
                        this.GetMenus(currentLanguageId),
                        this.GetCurrentLanguage(currentLanguageId),
                        Pagination.ToPagedList<MenuItem>(this._menuItemDAO.FindByMenuIdAndDepth(menuId.Value, 0), page ?? 1),
                        this.GetMenuTypesToDropDown(null),
                        this.GetCurrentMenu(menuId),
                        null, //item
                        page);  
            }   //if menu id
            return new MenusIndexViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                        this.GetPageTypesInMenu(currentLanguageId),
                        this.GetMenus(currentLanguageId),
                        this.GetCurrentLanguage(currentLanguageId),
                        null,   //menu items
                        this.GetMenuTypesToDropDown(null),
                        null,   //current menu
                        null,  //current item
                        page);
        }

        /// <summary>
        /// Metoda pomocnicza do pobierania modelu <code>MenusEditionViewModel</code>.
        /// </summary>
        /// <param name="menuItemToOperateWith">biezaca pozycja menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <param name="currentLanguageId">strona w paginacji</param>
        /// <returns>odpowiedni model danych</returns>
        private MenusEditionViewModel GetMenusEditionViewModel(MenuItem menuItemToOperateWith, int? menuId, 
            int? currentLanguageId, int? paginationPage)
        {
            if (paginationPage == null)
            {
                paginationPage = 1;
            }   //if null
            if (menuItemToOperateWith == null)
            {
                throw new Exception("MenusController exception: GetMenusEditionViewModel method, pageToOperateWith object null...");
            }   //if null
            if (!currentLanguageId.HasValue)
            {
                throw new Exception("MenusController exception: GetMenusEditionViewModel method, currentLanguageId null...");
            }   //if null
            if (!menuId.HasValue)
            {
                throw new Exception("MenusController exception: GetMenusEditionViewModel method, menuId null...");
            }   //if null
            return new MenusEditionViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                            this.GetPageTypesInMenu(currentLanguageId),
                            this.GetMenus(currentLanguageId),
                            this.GetCurrentLanguage(currentLanguageId),
                            this._menuDAO.FindByPK(menuId),
                            menuItemToOperateWith,
                            paginationPage.Value);
        }

        /// <summary>
        /// Metoda pomocnicza do pobierania modelu <code>MenusEditionViewModel</code>.
        /// </summary>
        /// <param name="menuItemToOperateWith">biezaca pozycja menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <param name="currentLanguageId">strona w paginacji</param>
        /// <param name="menuId">id menu</param>
        /// <param name="selectedPageTypeId">wybrana w combie grupa stron</param>
        /// <returns>odpowiedni model danych</returns>
        private MenusEditionViewModel GetMenusEditionViewModelWithPages(MenuItem menuItemToOperateWith, int? menuId, 
            int? currentLanguageId, int? paginationPage, int selectedPageTypeId)
        {
            if (paginationPage == null)
            {
                paginationPage = 1;
            }   //if null
            if (menuItemToOperateWith == null)
            {
                throw new Exception("MenusController exception: GetMenusEditionViewModelWithPages method, pageToOperateWith object null...");
            }   //if null
            if (!currentLanguageId.HasValue)
            {
                throw new Exception("MenusController exception: GetMenusEditionViewModelWithPages method, currentLanguageId null...");
            }   //if null
            if (!menuId.HasValue)
            {
                throw new Exception("MenusController exception: GetMenusEditionViewModelWithPages method, menuId null...");
            }   //if null
            IQueryable<PageType> pageTypes = this.GetPageTypesInMenu(currentLanguageId);
            if (pageTypes == null)
            {
                return new MenusEditionViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                                pageTypes,
                                this.GetMenus(currentLanguageId),
                                this.GetCurrentLanguage(currentLanguageId),
                                this._menuDAO.FindByPK(menuId),
                                menuItemToOperateWith,
                                paginationPage.Value);
            }   //if null
            IQueryable<Page> pages = null;
            if (selectedPageTypeId <= 0)
            {
                pages = this._pageDAO.FindByPageType(pageTypes.First().page_type_id);
            } else {  //if no page type
                pages = this._pageDAO.FindByPageType(selectedPageTypeId);
            }   //else no page type
            if (pages == null)
            {
                return new MenusEditionViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                                pageTypes,
                                this.GetMenus(currentLanguageId),
                                this.GetCurrentLanguage(currentLanguageId),
                                this._menuDAO.FindByPK(menuId),
                                menuItemToOperateWith,
                                paginationPage.Value);
            }   //if null
            return new MenusEditionViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                            pageTypes,
                            this.GetMenus(currentLanguageId),
                            this.GetCurrentLanguage(currentLanguageId),
                            this._menuDAO.FindByPK(menuId),
                            menuItemToOperateWith, 
                            pageTypes.ToSelectListItems(selectedPageTypeId),
                            selectedPageTypeId,
                            pages.ToSelectListItems(0),
                            0,  //selected page id
                            paginationPage.Value);
        }

        /// <summary>
        /// Metoda pomocnicza do pobierania modelu <code>MenusEditionViewModel</code>.
        /// </summary>
        /// <param name="menuItemToOperateWith">biezaca pozycja menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <param name="currentLanguageId">strona w paginacji</param>
        /// <param name="menuId">id menu</param>
        /// <param name="selectedPage">wybrana w combie strona</param>
        /// <returns>odpowiedni model danych</returns>
        private MenusEditionViewModel GetMenusEditionViewModelWithPages(MenuItem menuItemToOperateWith, int? menuId,
            int? currentLanguageId, int? paginationPage, Page selectedPage)
        {
            if (paginationPage == null)
            {
                paginationPage = 1;
            }   //if null
            if (menuItemToOperateWith == null)
            {
                throw new Exception("MenusController exception: GetMenusEditionViewModelWithPages method, pageToOperateWith object null...");
            }   //if null
            if (!currentLanguageId.HasValue)
            {
                throw new Exception("MenusController exception: GetMenusEditionViewModelWithPages method, currentLanguageId null...");
            }   //if null
            if (!menuId.HasValue)
            {
                throw new Exception("MenusController exception: GetMenusEditionViewModelWithPages method, menuId null...");
            }   //if null
            if (selectedPage == null)
            {
                throw new Exception("MenusController exception: GetMenusEditionViewModelWithPages method, selectedPage null...");
            }   //if null
            IQueryable<PageType> pageTypes = this.GetPageTypesInMenu(currentLanguageId);
            if (pageTypes == null)
            {
                return new MenusEditionViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                                pageTypes,
                                this.GetMenus(currentLanguageId),
                                this.GetCurrentLanguage(currentLanguageId),
                                this._menuDAO.FindByPK(menuId),
                                menuItemToOperateWith,
                                paginationPage.Value);
            }   //if null
            IQueryable<Page> pages = null;
            if (!selectedPage.page_type_id.HasValue)
            {
                pages = this._pageDAO.FindByPageType(pageTypes.First().page_type_id);
            }
            else
            {  //if no page type
                pages = this._pageDAO.FindByPageType(selectedPage.page_type_id.Value);
            }   //else no page type
            if (pages == null)
            {
                return new MenusEditionViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                                pageTypes,
                                this.GetMenus(currentLanguageId),
                                this.GetCurrentLanguage(currentLanguageId),
                                this._menuDAO.FindByPK(menuId),
                                menuItemToOperateWith,
                                paginationPage.Value);
            }   //if null
            return new MenusEditionViewModel(this.GetLanguagesToDropDown(currentLanguageId),
                            pageTypes,
                            this.GetMenus(currentLanguageId),
                            this.GetCurrentLanguage(currentLanguageId),
                            this._menuDAO.FindByPK(menuId),
                            menuItemToOperateWith,
                            pageTypes.ToSelectListItems(selectedPage.page_type_id),
                            selectedPage.page_type_id.Value,
                            pages.ToSelectListItems(selectedPage.page_id),
                            selectedPage.page_id,
                            paginationPage.Value);
        }

        /// <summary>
        /// Metoda pobierajaca typy pozycji menu umieszczane w drop downie na gorze strony.
        /// </summary>
        /// <param name="selectedId">aktualnie wybrany typ pozycji menu</param>
        /// <returns>typy pozycji menu umieszczane w drop downie</returns>
        private IEnumerable<SelectListItem> GetMenuTypesToDropDown(int? selectedId)
        {
            if (this._menuItemTypeDAO == null)
            {
                return null;
            }   //if null
            IQueryable<MenuItemType> menuItemTypes = this._menuItemTypeDAO.FindAll();
            if (menuItemTypes == null)
            {
                return null;
            }   //if null
            return menuItemTypes.ToSelectListItems(selectedId);
        }

        /// <summary>
        /// Metoda pobierajaca aktualne menu.
        /// </summary>
        /// <param name="menuId">id aktualnego menu</param>
        /// <returns>aktualna grupa typów pozycji menu</returns>
        private Menu GetCurrentMenu(int? menuId)
        {
            if (!menuId.HasValue)
            {
                return null;
            }   //if
            return this._menuDAO.FindByPK(menuId);
        }

        /// <summary>
        /// Metoda pobierajaca aktualny węzeł pozycji menu.
        /// </summary>
        /// <param name="menuItemId">id aktualnej pozycji menu</param>
        /// <returns>aktualna pozycja w menu</returns>
        private MenuItem GetCurrentMenuItem(int? menuItemId)
        {
            if (!menuItemId.HasValue)
            {
                return null;
            }   //if
            return this._menuItemDAO.FindByPK(menuItemId);
        }

        /// <summary>
        /// Akcja dla wyswietlania zawartości menu z poziomu wyżej.
        /// </summary>
        /// <param name="viewModel">biezacy widok</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult ToUp(MenusIndexViewModel viewModel)
        {
            try
            {
                if (viewModel == null)
                {
                    Logger.MyLogger.Log("MenusController exception: ToUp method, viewModel object null...");
                    return View("MenusError", this.GetBaseViewModel(null));
                }   //if null            
                if (viewModel.CurrentMenu != null)
                {
                    if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), viewModel.CurrentMenu.language_id))
                    {
                        return this.GetAccessDeniedView();
                    }   //if
                    if (viewModel.CurrentMenuItem != null)
                    {
                        MenuItem itemParent = _menuItemDAO.FindByPK(viewModel.CurrentMenuItem.parent_item_id);
                        if (itemParent != null)
                        {
                            return View("Index",
                                    this.GetMenusIndexViewModel(viewModel.CurrentMenu.menu_id, viewModel.CurrentMenuItem.parent_item_id, //itemParent.item_id,
                                    viewModel.CurrentLanguage.language_id));
                        }   //if parent
                    }   //if item
                    return View("Index",
                        this.GetMenusIndexViewModel(viewModel.CurrentMenu.menu_id,
                        viewModel.CurrentLanguage.language_id));
                }   //if current menu
                return View("Index", 
                    this.GetMenusIndexViewModel(viewModel.CurrentLanguage.language_id));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                if (viewModel.CurrentLanguage != null)
                {
                    return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("Errors/MenusError", this.GetBaseViewModel(null));
            }   //catch
        }


        /// <summary>
        /// Akcja dla wyswietlania zawartości menu.
        /// </summary>
        /// <param name="currentPage">biezaca strona paginacji</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult Index(int? page, int? menuId, int? menuItemId, int? currentLanguageId)
        {
            try
            {
                if (menuItemId <= 0)
                {
                    menuItemId = null;
                }   //if
                if (page <= 0)
                {
                    page = 1;
                }   //if
                if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), currentLanguageId))
                {
                    return this.GetAccessDeniedView();
                }   //if
                if ((page.HasValue) && (menuId.HasValue) && (menuItemId.HasValue) && (currentLanguageId.HasValue))
                {
                    return View(this.GetMenusIndexViewModel(page, menuId, menuItemId, currentLanguageId));
                }   //if all
                if ((menuId.HasValue) && (menuItemId.HasValue) && (currentLanguageId.HasValue))
                {
                    return View(this.GetMenusIndexViewModel(menuId, menuItemId, currentLanguageId));
                }   //bez page
                if ((menuId.HasValue) && (currentLanguageId.HasValue))
                {
                    return View(this.GetMenusIndexViewModel(page, menuId, null, currentLanguageId));
                }   //bez item
                Language currentLanguage = null;
                if (!currentLanguageId.HasValue)
                {
                    currentLanguage = this._languageDAO.GetDefaultLanguage(this.GetAuthenticatedUserId());
                    if (currentLanguage != null)
                    {
                        currentLanguageId = currentLanguage.language_id;
                    }   //if
                }
                else
                {   //if lang id null
                    currentLanguage = this._languageDAO.FindByPK(currentLanguageId);
                }   //else lang id null
                Menu menu = this._menuDAO.GetDefaultMenu(currentLanguage);
                if (menu != null)
                {
                    menuId = menu.menu_id;
                }   //if
                return View(this.GetMenusIndexViewModel(menuId, currentLanguageId));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
            }   //catch
        }

        /// <summary>
        /// Metoda odpowiadajaca za operacje zmiany wersji jezykowej w drop downie.
        /// </summary>
        /// <param name="viewModel">model danych</param>
        /// <returns>odpowiedni widok</returns>
        [Authorize]
        public ActionResult ChangeLanguage(MenusIndexViewModel viewModel)
        {
            try
            {
                if (viewModel == null)
                {
                    Logger.MyLogger.Log("MenusController exception: ChangeLanguage method, viewModel object null...");
                    return View("MenusError", this.GetBaseViewModel(null));
                }   //if null
                return View("Index", this.GetMenusIndexViewModel(viewModel.DropDownLanguageId));
            }   //try               
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                if (viewModel.CurrentLanguage != null)
                {
                    return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("Errors/MenusError", this.GetBaseViewModel(null));
            }   //catch
        }

        /// <summary>
        /// Tworzenie nowej pozycji menu.
        /// </summary>
        /// <param name="viewModel">model danych</param>
        /// <returns>widok</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult Create(MenusIndexViewModel viewModel)
        {
            try
            {
                if (viewModel == null)
                {
                    Logger.MyLogger.Log("MenusController exception: Create method, viewModel object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(null));
                }   //if null
                if (viewModel.CurrentLanguage == null)
                {
                    Logger.MyLogger.Log("MenusController exception: Create method, viewModel.CurrentLanguage object null...");
                    return View("Errors/MenusErrorLanguageUndefined", this.GetBaseViewModel(null));
                }   //if null
                if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), viewModel.CurrentLanguage.language_id))
                {
                    return this.GetAccessDeniedView();
                }   //if
                if (viewModel.CurrentMenu == null)
                {
                    Logger.MyLogger.Log("MenusController exception: Create method, viewModel.CurrentMenu object null...");
                    return View("Errors/MenusErrorMenuUndefined", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if null
                MenuItem newMenuItem = null;
                if (viewModel.CurrentMenuItem == null)
                {
                    newMenuItem = this.GetNewMenuItem(viewModel.CurrentMenu.menu_id, 
                        viewModel.DropDownMenuItemTypeId, null);
                }    //if no parent
                else
                {
                    newMenuItem = this.GetNewMenuItem(viewModel.CurrentMenu.menu_id, 
                        viewModel.DropDownMenuItemTypeId, 
                        viewModel.CurrentMenuItem.item_id);
                }   //else no parent
                if (newMenuItem == null)
                {
                    Logger.MyLogger.Log("MenusController exception: Create method, newMenuItem object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if null
                if (newMenuItem.EditionWithPageCombos())
                {
                    PageType defaultPageType = this._pageTypeDAO.GetDefaultPageType(viewModel.CurrentLanguage);
                    if (defaultPageType != null)
                    {
                        string itemLink = this.GenerateMenuItemLink(defaultPageType.page_type_id);
                        if (!String.IsNullOrEmpty(itemLink))
                        {
                            newMenuItem.item_link = itemLink;
                        }   //if
                        return View(this.GetMenusEditionViewModelWithPages(newMenuItem, 
                            viewModel.CurrentMenu.menu_id,
                            viewModel.CurrentLanguage.language_id, 
                            viewModel.PaginationPage,
                            defaultPageType.page_type_id));
                    }   //if
                    return View(this.GetMenusEditionViewModelWithPages(newMenuItem,
                        viewModel.CurrentMenu.menu_id,
                        viewModel.CurrentLanguage.language_id,
                        viewModel.PaginationPage,
                        0));
                }   //if page combos
                return View(this.GetMenusEditionViewModel(newMenuItem,
                    viewModel.CurrentMenu.menu_id,
                    viewModel.CurrentLanguage.language_id,
                    viewModel.PaginationPage));
            }   //try               
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                if (viewModel.CurrentLanguage != null)
                {
                    return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("Errors/MenusError", this.GetBaseViewModel(null));
            }   //catch
        }

        /// <summary>
        /// Akcja do zapisu nowego elementu menu.
        /// </summary>
        /// <param name="viewModel">model danych</param>
        /// <param name="collection">parametry z forma</param>
        /// <returns>widok</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateOnlyIncomingValues]
        [ValidateInput(false)]
        [Authorize]
        public ActionResult CreateNewMenuItem(MenusEditionViewModel viewModel, FormCollection formParams)
        {
            if (viewModel == null)
            {
                Logger.MyLogger.Log("MenusController exception: Create method, viewModel object null...");
                return View("Errors/MenusError", this.GetBaseViewModel(null));
            }   //if null
            if (viewModel.CurrentLanguage == null)
            {
                Logger.MyLogger.Log("MenusController exception: Create method, viewModel.CurrentLanguage object null...");
                return View("Errors/MenusErrorLanguageUndefined", this.GetBaseViewModel(null));
            }   //if null
            if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), viewModel.CurrentLanguage.language_id))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (viewModel.CurrentMenu == null)
            {
                Logger.MyLogger.Log("MenusController exception: Create method, viewModel.CurrentMenu object null...");
                return View("Errors/MenusErrorMenuUndefined", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
            }   //if null
            if (viewModel.MenuItemToOperateWith == null)
            {
                Logger.MyLogger.Log("MenusController exception: CreateNewMenuItem method, MenuItemToOperateWith object null...");
                return View("Errors/MenusErrorNoMenuItemFound", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
            }   //if null
            try
            {
                if (this.IsPageTypeChanged(formParams))
                {
                    string itemLink = this.GenerateMenuItemLink(viewModel.DropDownPageTypeId);
                    if (!String.IsNullOrEmpty(itemLink))
                    {
                        viewModel.MenuItemToOperateWith.item_link = itemLink;
                    }   //if
                    return View("Create", this.GetMenusEditionViewModelWithPages(viewModel.MenuItemToOperateWith,
                        viewModel.CurrentMenu.menu_id,
                        viewModel.CurrentLanguage.language_id,
                        viewModel.PaginationPage,
                        viewModel.DropDownPageTypeId));
                }   //if page type changed
                if (ModelState.IsValid)
                {
                    if (String.IsNullOrEmpty(viewModel.MenuItemToOperateWith.item_link))
                    {
                        viewModel.MenuItemToOperateWith.page_id = viewModel.DropDownPageId;
                    }   //if
                    MenuItem menuItemToUpdate = this.GetMenuItemToUpdate(viewModel.MenuItemToOperateWith);
                    if (menuItemToUpdate == null)
                    {
                        return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                    }
                    if (this.AreDatabaseConstraintsValid(menuItemToUpdate))
                    {
                        if (!this._menuItemDAO.SubmitChanges())
                        {
                            return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                        }   //if
                        return View("Index", this.GetMenusIndexViewModel(viewModel.PaginationPage,
                            viewModel.CurrentMenu.menu_id,
                            viewModel.MenuItemToOperateWith.parent_item_id,
                            viewModel.CurrentLanguage.language_id));
                    }   //if valid
                }   //if valid
                if (viewModel.MenuItemToOperateWith.EditionWithPageCombos())
                {
                    return View("Create", this.GetMenusEditionViewModelWithPages(viewModel.MenuItemToOperateWith,
                        viewModel.CurrentMenu.menu_id,
                        viewModel.CurrentLanguage.language_id,
                        viewModel.PaginationPage,
                        0));
                }   //if page combos
                return View("Create", this.GetMenusEditionViewModel(viewModel.MenuItemToOperateWith,
                    viewModel.CurrentMenu.menu_id,
                    viewModel.CurrentLanguage.language_id,
                    viewModel.PaginationPage));
            }   //try
            catch (Exception ex)            
            {
                Logger.MyLogger.Log(ex);
                if (viewModel.CurrentLanguage != null)
                {
                    return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("Errors/MenusError", this.GetBaseViewModel(null));
            }   //catch
        }

        /// <summary>
        /// Metoda okreslajaca czy przekierowanie przyszlo po zmianie grupy stron, czy po zwyklym submicie.
        /// </summary>
        /// <param name="formParams">parametry forma</param>
        /// <returns>true, gdy zmiana grupy stron</returns>
        private bool IsPageTypeChanged(FormCollection formParams)
        {
            if (formParams == null)
            {
                return false;
            }   //if
            string pageTypeChangedParam = formParams.Get(MenusConsts.MENU_ITEM_EDIT_PAGE_TYPE_CHANGED_PARAM_NAME);
            if (pageTypeChangedParam != null)
            {
                return pageTypeChangedParam.ToLower().Equals(
                    MenusConsts.MENU_ITEM_EDIT_PAGE_TYPE_CHANGED_PARAM_VALUE.ToLower());
            }   //if
            return false;
        }

        /// <summary>
        /// Metoda do aktualizacji pozycji menu w bazie na podstawie zadania.
        /// </summary>
        /// <param name="menuItemFromRequest">pozycja menu z modelu danych z zadania</param>
        /// <returns>element ze zaktualizowanymi danymi</returns>
        private MenuItem GetMenuItemToUpdate(MenuItem menuItemFromRequest)
        {
            if (menuItemFromRequest == null)
            {
                return null;
            }   //if null
            MenuItem menuItemToUpdate = this._menuItemDAO.FindByPK(menuItemFromRequest.item_id);
            if (menuItemToUpdate == null)
            {
                Logger.MyLogger.Log("MenusController exception: UpdateMenuItem method, unable to find menu item to update...");
                return null;
            }   //if null
            menuItemToUpdate.item_name = menuItemFromRequest.item_name;
            menuItemToUpdate.item_alias = this.GenerateMenuItemAlias(menuItemFromRequest);
            menuItemToUpdate.item_path = this.GenerateMenuItemPath(menuItemFromRequest);
            if (String.IsNullOrEmpty(menuItemFromRequest.item_link))
            {
                menuItemToUpdate.page_id = menuItemFromRequest.page_id;
                menuItemToUpdate.item_link = this.GenerateMenuItemLink(this.GetPage(menuItemFromRequest.page_id));
            }   //if link empty
            else
            {
                menuItemToUpdate.page_id = null;
                menuItemToUpdate.item_link = menuItemFromRequest.item_link;
            }   //else link empty
            return menuItemToUpdate;
        }

        /// <summary>
        /// Tworzenie nowego elementu w menu.
        /// </summary>
        /// <param name="menuId">menu</param>
        /// <param name="itemTypeId">typ elementu</param>
        /// <param name="parentItemId">rodzic (null, gdy element jest rootem)</param>
        /// <returns>nowy element lub null</returns>
        private MenuItem GetNewMenuItem(int? menuId, int? itemTypeId, int? parentItemId)
        {
            try
            {
                if ((!menuId.HasValue) || (!itemTypeId.HasValue))
                {
                    Logger.MyLogger.Log("MenusController exception: GetNewMenuItem method, menuId or itemTypeId null...");
                    return null;
                }   //if
                MenuItem menuItem = new MenuItem();
                menuItem.item_created = DateTime.Now;
                menuItem.item_modified = DateTime.Now;
                menuItem.item_editable = true;
                menuItem.menu_id = menuId;
                menuItem.item_name = MenusConsts.DEFAULT_MENU_ITEM_NAME + " " + this.GetMaxMenuItemId();
                menuItem.item_alias = this.GenerateMenuItemAlias(menuItem);
                if (parentItemId.HasValue)
                {
                    if (parentItemId > 0)
                    {
                        menuItem.parent_item_id = parentItemId;
                    }   //if parent
                }   //if parent
                menuItem.item_published = false;
                menuItem.item_removable = true;
                menuItem.item_type_id = itemTypeId.Value;
                menuItem.item_ordering = this.GetMaxOrdering(menuId.Value) + 1;
                menuItem.item_path = this.GenerateMenuItemPath(menuItem);
                menuItem.item_depth = this.GenerateMenuItemDepth(menuItem);
                menuItem.item_link = String.Empty;
                return this._menuItemDAO.Add(menuItem);
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return null;
            }   //catch            
        }

        /// <summary>
        /// Metoda pobierajaca najwyzszy porzadek dla sortowania pozycji menu.
        /// </summary>
        /// <param name="menuId">menu</param>
        /// <returns>najwyzszy porzadek dla sortowania pozycji menu</returns>
        private int GetMaxOrdering(int menuId)
        {
            if (this._menuItemDAO == null)
            {
                return 0;
            }   //if
            int? maxOrdering = this._menuItemDAO.SelectMaxItemOrdering(menuId);
            if ((maxOrdering < 0) || (maxOrdering == null))
            {
                maxOrdering = 0;
            }   //if
            return maxOrdering.Value;
        }

        /// <summary>
        /// Metoda pobierajaca pozycje menu z bazy danych.
        /// </summary>
        /// <param name="menuItemId">id pozycji menu</param>
        /// <returns>obiekt strony</returns>
        private MenuItem GetMenuItem(int? menuItemId)
        {
            try
            {
                if (!menuItemId.HasValue)
                {
                    Logger.MyLogger.Log("MenusController exception: GetMenuItem method, menuItemId null...");
                    return null;
                }   //if
                if (this._menuItemDAO == null)
                {
                    Logger.MyLogger.Log("MenussController exception: GetMenuItem method, menuItemDAO null...");
                    return null;
                }   //if
                return this._menuItemDAO.FindByPK(menuItemId);
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return null;
            }   //catch            
        }

        /// <summary>
        /// Zamiana porzadku dwoch pozycji menu.
        /// </summary>
        /// <param name="firstMenuItemId">wersja jezykowa</param>
        /// <param name="secondMenuItemId">id menu</param>
        /// <returns>widok</returns>
        private bool SwitchMenuItemOrdering(int? firstMenuItemId, int? secondMenuItemId)
        {
            if (firstMenuItemId == null)
            {
                Logger.MyLogger.Log("MenusController exception: menuItemSwitch method, firstMenuItemId object null...");
                return false;
            }   //if null
            if (secondMenuItemId == null)
            {
                Logger.MyLogger.Log("MenusController exception: menuItemSwitch method, secondMenuItemId object null...");
                return false;
            }   //if null
            return this._menuItemDAO.SwitchItemOrder(firstMenuItemId ?? 1, secondMenuItemId ?? 1);
        }

        /// <summary>
        /// Przesuniecie pozycji menu o jedno w gore.
        /// </summary>
        /// <param name="currentLanguageId">wersja jezykowa</param>
        /// <param name="currentMenuId">id menu</param>
        /// <param name="currentMenuItemId">id pozycji menu nadrzednej (null jesli brak)</param>
        /// <param name="menuItemToMoveId">id pozycji menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult MenuItemOneUp(int? menuItemToMoveId, int? currentMenuId, int? currentMenuItemId, int? currentLanguageId, int? paginationPage)
        {
            try
            {
                if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), currentLanguageId))
                {
                    return this.GetAccessDeniedView();
                }   //if
                if (currentLanguageId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemOneUp method, currentLanguageId object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(null));
                }   //if null
                if (menuItemToMoveId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemOneUp method, menuItemToMoveId object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(null));
                }   //if null
                MenuItem menuItem = this.GetMenuItem(menuItemToMoveId);
                if (menuItem == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemOneUp method, menuItem object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
                }   //if null
	            if (this._menuItemDAO == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemOneUp method, menuItemDAO null...");
                    return null;
                }   //if
	            MenuItem menuItemToSwitch = this._menuItemDAO.FindNeighbourByMenuIdAndParentItemIdAndItemOrderingLower(currentMenuId ?? 1, currentMenuItemId, menuItem.item_ordering);
	            if (menuItemToSwitch == null)
    	        {
	    	        Logger.MyLogger.Log("MenusController exception: MenuItemOneUp method, menuItemToSwitch object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
    	        }
                if (this.SwitchMenuItemOrdering(menuItem.item_id, menuItemToSwitch.item_id))
                {
                    return View("Index", this.GetMenusIndexViewModel(paginationPage, currentMenuId, currentMenuItemId, currentLanguageId));
                }   //if
                return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
            }   //try               
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
            }   //catch
        }


        /// <summary>
        /// Przesuniecie pozycji menu o jedno w dol.
        /// </summary>
        /// <param name="currentLanguageId">wersja jezykowa</param>
        /// <param name="currentMenuId">id menu</param>
        /// <param name="currentMenuItemId">id pozycji menu nadrzednej (null jesli brak)</param>
        /// <param name="menuItemToMoveId">id pozycji menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult MenuItemOneDown(int? menuItemToMoveId, int? currentMenuId, int? currentMenuItemId, int? currentLanguageId, int? paginationPage)
        {
            try
            {
                if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), currentLanguageId))
                {
                    return this.GetAccessDeniedView();
                }   //if
                if (currentLanguageId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemOneUp method, currentLanguageId object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(null));
                }   //if null
                MenuItem menuItem = this.GetMenuItem(menuItemToMoveId);
                if (menuItem == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemOneUp method, menuItem object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
                }   //if null
 	            if (this._menuItemDAO == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemOneDown method, menuItemDAO null...");
                    return null;
                }   //if
	            MenuItem menuItemToSwitch = this._menuItemDAO.FindNeighbourByMenuIdAndParentItemIdAndItemOrderingHigher(currentMenuId ?? 1, currentMenuItemId, menuItem.item_ordering);
	            if (menuItemToSwitch == null)
	            {
		            Logger.MyLogger.Log("MenusController exception: MenuItemOneUp method, menuItemToSwitch object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
	            }
                if (this.SwitchMenuItemOrdering(menuItem.item_id, menuItemToSwitch.item_id))
                {
                    return View("Index", this.GetMenusIndexViewModel(paginationPage, currentMenuId, currentMenuItemId, currentLanguageId));
                }   //if
                return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
            }   //try               
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
            }   //catch
        }

        /// <summary>
        /// Przesuniecie pozycji menu na sama gore.
        /// </summary>
        /// <param name="currentLanguageId">wersja jezykowa</param>
        /// <param name="currentMenuId">id menu</param>
        /// <param name="currentMenuItemId">id pozycji menu nadrzednej (null jesli brak)</param>
        /// <param name="menuItemToMoveId">id pozycji menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult MenuItemTopUp(int? menuItemToMoveId, int? currentMenuId, int? currentMenuItemId, int? currentLanguageId, int? paginationPage)
        {
            try
            {
                if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), currentLanguageId))
                {
                    return this.GetAccessDeniedView();
                }   //if
                if (currentLanguageId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemTopUp method, currentLanguageId object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(null));
                }   //if null
                if (menuItemToMoveId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemTopUp method, menuItemToMoveId object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(null));
                }   //if null
                MenuItem menuItem = this.GetMenuItem(menuItemToMoveId);
                if (menuItem == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemTopUp method, menuItem object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
                }   //if null
                if (this._menuItemDAO == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemTopUp method, menuItemDAO null...");
                    return null;
                }   //if
                IQueryable<MenuItem> menuItemsToSwitch = this._menuItemDAO.FindByMenuIdAndParentItemIdAndItemOrderingLower(currentMenuId ?? 1, currentMenuItemId, menuItem.item_ordering);
                if (!menuItemsToSwitch.Any())
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemTopUp method, menuItemToSwitch object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
                }
                if (!_menuItemDAO.SwitchItemsOrder(menuItemsToSwitch, menuItem.item_id))
                {
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));               
                }   //if
                return View("Index", this.GetMenusIndexViewModel(paginationPage, currentMenuId, currentMenuItemId, currentLanguageId));
            }   //try               
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
            }   //catch
        }

        /// <summary>
        /// Przesuniecie pozycji menu na sam dol.
        /// </summary>
        /// <param name="currentLanguageId">wersja jezykowa</param>
        /// <param name="currentMenuId">id menu</param>
        /// <param name="currentMenuItemId">id pozycji menu nadrzednej (null jesli brak)</param>
        /// <param name="menuItemToMoveId">id pozycji menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult MenuItemBottomDown(int? menuItemToMoveId, int? currentMenuId, int? currentMenuItemId, int? currentLanguageId, int? paginationPage)
        {
            try
            {
                if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), currentLanguageId))
                {
                    return this.GetAccessDeniedView();
                }   //if
                if (currentLanguageId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemTopUp method, currentLanguageId object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(null));
                }   //if null
                if (menuItemToMoveId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemTopUp method, menuItemToMoveId object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(null));
                }   //if null
                MenuItem menuItem = this.GetMenuItem(menuItemToMoveId);
                if (menuItem == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemTopUp method, menuItem object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
                }   //if null
                if (this._menuItemDAO == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemTopUp method, menuItemDAO null...");
                    return null;
                }   //if
                IQueryable<MenuItem> menuItemsToSwitch = this._menuItemDAO.FindByMenuIdAndParentItemIdAndItemOrderingHigher(currentMenuId ?? 1, currentMenuItemId, menuItem.item_ordering);
                if (!menuItemsToSwitch.Any())
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemTopUp method, menuItemToSwitch object null...");
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
                }
                if (!_menuItemDAO.SwitchItemsOrder(menuItemsToSwitch, menuItem.item_id))
                {
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
                }   //if
                return View("Index", this.GetMenusIndexViewModel(paginationPage, currentMenuId, currentMenuItemId, currentLanguageId));
            }   //try               
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
            }   //catch
        }

        /// <summary>
        /// Metoda do generowania sciezki do edytowanej/wprowadzanej pozycji menu.
        /// </summary>
        /// <param name="menuItem">pozycja menu</param>
        /// <returns>sciezka</returns>
        private String GenerateMenuItemPath(MenuItem menuItem)
        {            
            if (menuItem == null)
            {
                throw new Exception("MenusController exception: GenerateMenuItemPath method, menuItem object null...");
            }   //if null
            string elementName = menuItem.item_name;
            List<String> pathList = new List<String>();
            String pathString = "[ ";
            while (menuItem.parent_item_id != null)
            {
                menuItem = this.GetMenuItem(menuItem.parent_item_id);
                if (menuItem != null)
                {
                    pathList.Add(menuItem.item_name.ToString());
                }   //if
            }   //while
            foreach (String str in pathList.Reverse<String>())
            {
                pathString += str + " / ";
            }   //foreach
            pathString += elementName;
            return pathString += " ]";
        }

        /// <summary>
        /// Metoda do generowania glebokosci edytowanej/wprowadzanej pozycji menu.
        /// </summary>
        /// <param name="menuItem">pozycja menu</param>
        /// <returns>glebokosc</returns>
        private int GenerateMenuItemDepth(MenuItem menuItem)
        {
            if (menuItem == null)
            {
                throw new Exception("MenusController exception: GenerateMenuItemDepth method, menuItem object null...");
            }   //if null
            int depth = 0;
            while (menuItem.parent_item_id != null)
            {
                menuItem = this.GetMenuItem(menuItem.parent_item_id);
                if (menuItem != null)
                {
                    depth++;
                }   //if
            }   //while
            return depth;
        }

        /// <summary>
        /// Metoda do generowania aliasu dla pozycji w menu.
        /// </summary>
        /// <param name="page">strona</param>
        /// <returns>alias</returns>
        private string GenerateMenuItemAlias(MenuItem menuItem)
        {
            if (menuItem == null)
            {
                throw new Exception("MenusController exception: GenerateMenuItemAlias method, menuItem object null...");
            }   //if null
            return menuItem.item_name.RemovePolishChars().RewriteToUrl();
        }

        /// <summary>
        /// Metoda do generowania linka dla pozycji w menu.
        /// </summary>
        /// <param name="page">strona</param>
        /// <returns>link</returns>
        private string GenerateMenuItemLink(Page page)
        {
            if (page == null)
            {
                throw new Exception("MenusController exception: GenerateMenuItemAlias method, page object null...");
            }   //if null
            return page.page_link;
        }

        /// <summary>
        /// Metoda do generowania linka dla pozycji w menu na podstawie pierwszej strony w grupie.
        /// </summary>
        /// <param name="pageTypeId">id grupy stron</param>
        /// <returns>link lub pusty lancuch, gdy brak stron</returns>
        private string GenerateMenuItemLink(int pageTypeId)
        {
            IQueryable<Page> pages = this._pageDAO.FindByPageType(pageTypeId);
            if (pages == null)
            {
                return String.Empty;
            }   //if
            if (!pages.Any())
            {
                return String.Empty;
            }   //if
            return pages.First().page_link;
        }

        /// <summary>
        /// Metoda pobierajaca strone z bazy danych.
        /// </summary>
        /// <param name="pageId">grupa stron</param>
        /// <returns>obiekt strony</returns>
        private Page GetPage(int? pageId)
        {
            try
            {
                if (!pageId.HasValue)
                {
                    Logger.MyLogger.Log("MenusController exception: GetPage method, pageId null...");
                    return null;
                }   //if
                if (this._pageDAO == null)
                {
                    Logger.MyLogger.Log("MenusController exception: GetPage method, pageDAO null...");
                    return null;
                }   //if
                return this._pageDAO.FindByPK(pageId);
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return null;
            }   //catch            
        }

        /// <summary>
        /// Metoda pobierajaca najwyzsze id dla elementu menu.
        /// </summary>
        /// <returns>najwyzsze id dla elementu menu</returns>
        private int GetMaxMenuItemId()
        {
            if (this._menuItemDAO == null)
            {
                return 1;
            }   //if
            int? maxId = this._menuItemDAO.SelectMaxMenuItemId();
            if (maxId == null)
            {
                return 1;
            }   //if
            return maxId.Value + 1;
        }

        /// <summary>
        /// Edycja elementu menu.
        /// </summary>
        /// <param name="currentLanguageId">wersja jezykowa</param>
        /// <param name="currentMenuId">menu</param>
        /// <param name="menuItemToEditId">id elementu w menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult EditMenuItem(int? menuItemToEditId, int? currentMenuId, int? currentLanguageId, int? paginationPage)
        {
            try
            {
                if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), currentLanguageId))
                {
                    return this.GetAccessDeniedView();
                }   //if
                if (currentLanguageId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: EditMenuItem method, currentLanguageId object null...");
                    return View("Errors/MenusErrorLanguageUndefined", this.GetBaseViewModel(null));
                }   //if null
                if (menuItemToEditId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: EditMenuItem method, menuItemToEditId object null...");
                    return View("Errors/MenusErrorNoMenuItemFound", this.GetBaseViewModel(currentLanguageId));
                }   //if null
                if (currentMenuId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: Create method, currentMenuId object null...");
                    return View("Errors/MenusErrorMenuUndefined", this.GetBaseViewModel(currentLanguageId));
                }   //if null
                MenuItem menuItem = this.GetMenuItem(menuItemToEditId);
                if (menuItem == null)
                {
                    Logger.MyLogger.Log("MenusController exception: EditMenuItem method, menuItem object null...");
                    return View("Errors/MenusErrorNoMenuItemFound", this.GetBaseViewModel(currentLanguageId));
                }   //if null
                if (menuItem.EditionWithPageCombos())
                {
                    return View(this.GetMenusEditionViewModelWithPages(menuItem,
                        currentMenuId,
                        currentLanguageId,
                        paginationPage,
                        this.GetPage(menuItem.page_id)));
                }   //if page combos
                return View(this.GetMenusEditionViewModel(menuItem,
                        currentMenuId,
                        currentLanguageId,
                        paginationPage));
            }   //try               
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
            }   //catch
        }

        /// <summary>
        /// Akcja do zapisu edytowanego elementu menu.
        /// </summary>
        /// <param name="viewModel">model danych</param>
        /// <param name="collection">parametry z forma</param>
        /// <returns>widok</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateOnlyIncomingValues]
        [ValidateInput(false)]
        [Authorize]
        public ActionResult SubmitMenuItem(MenusEditionViewModel viewModel, FormCollection formParams)
        {
            if (viewModel == null)
            {
                Logger.MyLogger.Log("MenusController exception: SubmitMenuItem method, viewModel object null...");
                return View("Errors/MenusError", this.GetBaseViewModel(null));
            }   //if null
            if (viewModel.CurrentLanguage == null)
            {
                Logger.MyLogger.Log("MenusController exception: SubmitMenuItem method, viewModel.CurrentLanguage object null...");
                return View("Errors/MenusErrorLanguageUndefined", this.GetBaseViewModel(null));
            }   //if null
            if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), viewModel.CurrentLanguage.language_id))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (viewModel.CurrentMenu == null)
            {
                Logger.MyLogger.Log("MenusController exception: SubmitMenuItem method, viewModel.CurrentMenu object null...");
                return View("Errors/MenusErrorMenuUndefined", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
            }   //if null
            if (viewModel.MenuItemToOperateWith == null)
            {
                Logger.MyLogger.Log("MenusController exception: SubmitMenuItem method, MenuItemToOperateWith object null...");
                return View("Errors/MenusErrorNoMenuItemFound", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
            }   //if null
            try
            {
                if (this.IsPageTypeChanged(formParams))
                {
                    string itemLink = this.GenerateMenuItemLink(viewModel.DropDownPageTypeId);
                    if (!String.IsNullOrEmpty(itemLink))
                    {
                        viewModel.MenuItemToOperateWith.item_link = itemLink;
                    }   //if
                    return View("EditMenuItem", this.GetMenusEditionViewModelWithPages(viewModel.MenuItemToOperateWith,
                        viewModel.CurrentMenu.menu_id,
                        viewModel.CurrentLanguage.language_id,
                        viewModel.PaginationPage,
                        viewModel.DropDownPageTypeId));
                }   //if page type changed
                if (ModelState.IsValid)
                {
                    if (String.IsNullOrEmpty(viewModel.MenuItemToOperateWith.item_link))
                    {
                        viewModel.MenuItemToOperateWith.page_id = viewModel.DropDownPageId;
                    }   //if
                    MenuItem menuItemToUpdate = this.GetMenuItemToUpdate(viewModel.MenuItemToOperateWith);
                    if (menuItemToUpdate == null)
                    {
                        return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                    }
                    if (this.AreDatabaseConstraintsValid(menuItemToUpdate))
                    {
                        if (!this._menuItemDAO.SubmitChanges())
                        {
                            return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                        }   //if
                        return View("Index", this.GetMenusIndexViewModel(viewModel.PaginationPage,
                            viewModel.CurrentMenu.menu_id,
                            viewModel.MenuItemToOperateWith.parent_item_id,
                            viewModel.CurrentLanguage.language_id));
                    }   //if valid
                }   //if valid
                if (viewModel.MenuItemToOperateWith.EditionWithPageCombos())
                {
                    return View("EditMenuItem", this.GetMenusEditionViewModelWithPages(viewModel.MenuItemToOperateWith,
                        viewModel.CurrentMenu.menu_id,
                        viewModel.CurrentLanguage.language_id,
                        viewModel.PaginationPage,
                        0));
                }   //if page combos
                return View("EditMenuItem", this.GetMenusEditionViewModel(viewModel.MenuItemToOperateWith,
                    viewModel.CurrentMenu.menu_id,
                    viewModel.CurrentLanguage.language_id,
                    viewModel.PaginationPage));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                if (viewModel.CurrentLanguage != null)
                {
                    return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("Errors/MenusError", this.GetBaseViewModel(null));
            }   //catch
        }

        /// <summary>
        /// Metoda do ukrywania/publikacji elementu w menu.
        /// </summary>
        /// <param name="menuItemId">id elementu menu</param>
        private bool PublishOrUnpublishMenuItem(int? menuItemId)
        {
            try
            {
                MenuItem menuItem = this.GetMenuItem(menuItemId);
                if (menuItem == null)
                {
                    Logger.MyLogger.Log("MenusController exception: PublishOrUnpublishMenuItem method, menu item object null...");
                    return false;
                }   //if null
                menuItem.item_published = !menuItem.item_published;
                return this._menuItemDAO.SubmitChanges();
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return false;
            }   //catch
        }

        /// <summary>
        /// Metoda do ukrywania/publikacji strony na podstawie elementu w menu.
        /// </summary>
        /// <param name="menuItemId">id elementu menu</param>
        private bool PublishOrUnpublishPage(int? menuItemId)
        {
            try
            {
                if (menuItemId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: PublishOrUnpublishPage method, menuItemId null...");
                    return false;
                }   //if null
                MenuItem menuItem = this.GetMenuItem(menuItemId);
                if (menuItem == null)
                {
                    Logger.MyLogger.Log("MenusController exception: PublishOrUnpublishPage method, menu item object null...");
                    return false;
                }   //if null
                if (!menuItem.page_id.HasValue)
                {
                    return true;
                }   //if no page
                Page page = this.GetPage(menuItem.page_id.Value);
                if (page != null)
                {
                    page.page_published = menuItem.item_published;
                    if (!this._pageDAO.SubmitChanges())
                    {
                        Logger.MyLogger.Log("MenusController exception: PublishOrUnpublishPage method, unable to commit page published info...");
                    }   //if
                }   //if
                return true;
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return false;
            }   //catch
        }

        /// <summary>
        /// Ukrycie elementu w menu.
        /// </summary>
        /// <param name="currentLanguageId">wersja jezykowa</param>
        /// <param name="currentMenuId">menu</param>
        /// <param name="menuItemToHideId">id elementu w menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <param name="currentMenuItemId">parent</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult HideMenuItem(int? menuItemToHideId, int? currentMenuId, int? currentMenuItemId, 
            int? currentLanguageId, int? paginationPage)
        {
            try
            {
                if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), currentLanguageId))
                {
                    return this.GetAccessDeniedView();
                }   //if
                if (currentLanguageId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: HideMenuItem method, currentLanguageId object null...");
                    return View("Errors/MenusErrorLanguageUndefined", this.GetBaseViewModel(null));
                }   //if null
                if (currentMenuId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: HideMenuItem method, currentMenuId object null...");
                    return View("Errors/MenusErrorMenuUndefined", this.GetBaseViewModel(currentLanguageId));
                }   //if null
                if (menuItemToHideId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: HideMenuItem method, menuItemToHideId object null...");
                    return View("Errors/MenusErrorNoMenuItemFound", this.GetBaseViewModel(currentLanguageId));
                }   //if null
                if (!this.PublishOrUnpublishMenuItem(menuItemToHideId))
                {
                    Logger.MyLogger.Log("MenusController exception: PublishOrUnpublishMenuItem method, unable to commit menu item visibility info...");
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
                }   //if
                if (!this.PublishOrUnpublishPage(menuItemToHideId))
                {
                    Logger.MyLogger.Log("MenusController exception: PublishOrUnpublishPage method, unable to commit page visibility info...");
                    return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
                }   //if
                return View("Index", this.GetMenusIndexViewModel(paginationPage, currentMenuId, currentMenuItemId, currentLanguageId));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
            }   //catch
        }

        /// <summary>
        /// Pokazanie elementu w menu.
        /// </summary>
        /// <param name="currentLanguageId">wersja jezykowa</param>
        /// <param name="currentMenuId">menu</param>
        /// <param name="menuItemToPublishId">id elementu w menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <param name="currentMenuItemId">parent</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult PublishMenuItem(int? menuItemToPublishId, int? currentMenuId, int? currentMenuItemId,
            int? currentLanguageId, int? paginationPage)
        {
            if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), currentLanguageId))
            {
                return this.GetAccessDeniedView();
            }   //if
            return this.HideMenuItem(menuItemToPublishId, currentMenuId, currentMenuItemId, currentLanguageId, 
                paginationPage);
        }

        /// <summary>
        /// Metoda usuwajaca galaz drzewa.
        /// </summary>
        /// <param name="menuItem">rodzic, od ktorego zaczynamy usuwanie</param>
        /// <returns>true, gdy sie uda, false wpp</returns>
        private bool DeleteMenuItemRecursive(MenuItem menuItem)
        {
            try
            {
                if (menuItem == null)
                {
                    throw new Exception("MenusController exception: DeleteMenuItem method, menuItem object null...");
                }   //if null
                IQueryable<MenuItem> childMenuItems = this._menuItemDAO.FindByParentId(menuItem.item_id);
                if (childMenuItems != null)
                {
                    foreach (MenuItem childMenuItem in childMenuItems)
                    {
                        this.DeleteMenuItemRecursive(childMenuItem);
                    }   //foreach
                }   //if not null
                this._menuItemDAO.Delete(menuItem.item_id);
                return this._menuItemDAO.SubmitChanges();
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return false;
            }   //catch
        }

        /// <summary>
        /// Metoda sprawdzajaca czy sa elementy w menu zalezne od wybranego elementu.
        /// </summary>
        /// <param name="menuItemId">id strony</param>
        private bool MenuItemHasChildren(int? menuItemId)
        {
            try
            {
                if (menuItemId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: MenuItemHasChilder method, menuItemId null...");
                    return false;
                }   //if null
                return (this._menuItemDAO.FindByParentId(menuItemId.Value) != null);
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return false;
            }   //catch
        }

        /// <summary>
        /// Pokazanie ekranu do usuniecia elementu w menu.
        /// </summary>
        /// <param name="currentLanguageId">wersja jezykowa</param>
        /// <param name="currentMenuId">menu</param>
        /// <param name="menuItemToDeleteId">id elementu w menu</param>
        /// <param name="paginationPage">strona w paginacji</param>
        /// <param name="currentMenuItemId">parent</param>
        /// <returns>widok</returns>
        [Authorize]
        public ActionResult DeleteMenuItem(int? menuItemToDeleteId, int? currentMenuId, int? currentMenuItemId,
            int? currentLanguageId, int? paginationPage)
        {
            try
            {
                if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), currentLanguageId))
                {
                    return this.GetAccessDeniedView();
                }   //if
                if (currentLanguageId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: DeleteMenuItem method, currentLanguageId object null...");
                    return View("Errors/MenusErrorLanguageUndefined", this.GetBaseViewModel(null));
                }   //if null
                if (currentMenuId == null)
                {
                    Logger.MyLogger.Log("MenusController exception: DeleteMenuItem method, currentMenuId object null...");
                    return View("Errors/MenusErrorMenuUndefined", this.GetBaseViewModel(currentLanguageId));
                }   //if null
                if ((menuItemToDeleteId == null) || (menuItemToDeleteId <= 0))
                {
                    Logger.MyLogger.Log("MenusController exception: DeleteMenuItem method, menuItemToDeleteId null...");
                    return View("Errors/MenusErrorNoMenuItemFound", this.GetBaseViewModel(currentLanguageId));
                }   //if null
                MenuItem menuItem = this.GetMenuItem(menuItemToDeleteId);
                if (menuItem == null)
                {
                    Logger.MyLogger.Log("MenusController exception: DeleteMenuItem method, menuItem null...");
                    return View("Errors/MenusErrorNoMenuItemFound", this.GetBaseViewModel(currentLanguageId));
                }   //if null
                if (this.MenuItemHasChildren(menuItemToDeleteId))
                {
                    return View("DeleteMenuItemWithChildren", this.GetMenusEditionViewModel(menuItem, currentMenuId, 
                        currentLanguageId, paginationPage));
                }   //if
                return View(this.GetMenusEditionViewModel(menuItem, currentMenuId,
                    currentLanguageId, paginationPage));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return View("Errors/MenusError", this.GetBaseViewModel(currentLanguageId));
            }   //catch
        }

        /// <summary>
        /// Metoda usuwajaca element menu (cala galaz).
        /// </summary>
        /// <param name="viewModel">model danych</param>
        /// <returns>odpowiedni widok</returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult DeleteConfirmed(MenusEditionViewModel viewModel)
        {
            if (viewModel == null)
            {
                Logger.MyLogger.Log("MenusController exception: DeleteConfirmed method, viewModel object null...");
                return View("Errors/MenusError", this.GetBaseViewModel(null));
            }   //if null
            if (viewModel.CurrentLanguage == null)
            {
                Logger.MyLogger.Log("MenusController exception: DeleteConfirmed method, viewModel.CurrentLanguage object null...");
                return View("Errors/MenusErrorLanguageUndefined", this.GetBaseViewModel(null));
            }   //if null
            if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), viewModel.CurrentLanguage.language_id))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (viewModel.CurrentMenu == null)
            {
                Logger.MyLogger.Log("MenusController exception: DeleteConfirmed method, viewModel.CurrentMenu object null...");
                return View("Errors/MenusErrorMenuUndefined", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
            }   //if null
            if (viewModel.MenuItemToOperateWith == null)
            {
                Logger.MyLogger.Log("MenusController exception: DeleteConfirmed method, MenuItemToOperateWith object null...");
                return View("Errors/MenusErrorNoMenuItemFound", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
            }   //if null
            try
            {
                MenuItem menuItem = this.GetMenuItem(viewModel.MenuItemToOperateWith.item_id);
                if (menuItem == null)
                {
                    Logger.MyLogger.Log("MenusController exception: DeleteConfirmed method, menuItem object null...");
                    return View("Errors/MenusErrorNoMenuItemFound", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if null
                if (this.DeleteMenuItemRecursive(menuItem))
                {
                    return View("Index", this.GetMenusIndexViewModel(viewModel.PaginationPage,
                        viewModel.CurrentMenu.menu_id,
                        viewModel.MenuItemToOperateWith.parent_item_id,
                        viewModel.CurrentLanguage.language_id));
                }   //if delete
                return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
            }   //catch
        }

        /// <summary>
        /// Metoda walidujaca poprawnosc wprowadzanych danych pod katem indeksow bazodanowych.
        /// </summary>
        /// <param name="itemFromRequest">element do walidacji</param>
        /// <returns>true, gdy ok, false wpp</returns>
        private bool AreDatabaseConstraintsValid(MenuItem itemFromRequest)
        {
            if (itemFromRequest == null)
            {
                Logger.MyLogger.Log("MenusController exception: AreDatabaseConstraintsValid method, item from request null...");
                return false;
            }   //if null
            if (!this._menuItemDAO.IsAliasLinkMenuIdUnique(itemFromRequest.item_link, itemFromRequest.item_alias, 
                itemFromRequest.menu_id, itemFromRequest.item_id))
            {
                ModelState.AddModelError("MenuItemToOperateWith.item_name", "Wprowadzona nazwa elementu już istnieje dla wskazanego linka w bieżącym menu.");
                return false;
            }   //if
            return true;
        }

        /// <summary>
        /// Metoda anulujaca akcje dodawania nowego elementu. 
        /// </summary>
        /// <param name="viewModel">model danych</param>
        /// <returns>odpowiedni widok</returns>
        [Authorize]
        public ActionResult CancelNewItem(MenusEditionViewModel viewModel)
        {
            if (viewModel == null)
            {
                Logger.MyLogger.Log("MenusController exception: SubmitMenuItem method, viewModel object null...");
                return View("Errors/MenusError", this.GetBaseViewModel(null));
            }   //if null
            if (viewModel.CurrentLanguage == null)
            {
                Logger.MyLogger.Log("MenusController exception: SubmitMenuItem method, viewModel.CurrentLanguage object null...");
                return View("Errors/MenusErrorLanguageUndefined", this.GetBaseViewModel(null));
            }   //if null
            if (!PermissionHelper.IsUserLanguageAvailable(this.GetAuthenticatedUserId(), viewModel.CurrentLanguage.language_id))
            {
                return this.GetAccessDeniedView();
            }   //if
            if (viewModel.CurrentMenu == null)
            {
                Logger.MyLogger.Log("MenusController exception: SubmitMenuItem method, viewModel.CurrentMenu object null...");
                return View("Errors/MenusErrorMenuUndefined", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
            }   //if null
            if (viewModel.MenuItemToOperateWith == null)
            {
                Logger.MyLogger.Log("MenusController exception: SubmitMenuItem method, MenuItemToOperateWith object null...");
                return View("Errors/MenusErrorNoMenuItemFound", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
            }   //if null
            try
            {
                return this.DeleteConfirmed(viewModel);
            }   //try
            catch (Exception ex)
            {
                Logger.MyLogger.Log(ex);
                if (viewModel.CurrentLanguage != null)
                {
                    return View("Errors/MenusError", this.GetBaseViewModel(viewModel.CurrentLanguage.language_id));
                }   //if
                return View("Errors/MenusError", this.GetBaseViewModel(null));
            }   //catch
        }

    }
}
